using System;
using System.Diagnostics;
using Xfinity.Console;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace Xfinity.Debugging
{
    public class GCBeep
    {
        private static DateTime lastCollection;
        private static bool enabled = false;

        /// <summary>
        /// Enable or disable GC notifications.
        /// </summary>
        public static bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Xfinity.Debugging.GCBeep"), Conditional("DEBUG")]
        public static void EnableGarbageCollectionNotification()
        {
            if (Xfinity.Settings.EngineSettings.GarbageCollectionNotificationEnabled)
            {
                enabled = true;
                gen2 = GC.CollectionCount(2);
                gen1 = GC.CollectionCount(1);
                new GCBeep();
            }

        }
        [BindCommand("ToggleGarbageCollectionDebugging", DefaultBoundKey = Keys.PageUp)]
        public static void ToggleGCNotification(string[] commands)
        {
            if (enabled)
            {
                enabled = false;
            }
            else
            {
                //System.Diagnostics.Debug.Assert(false, "Garabge collection debugging enabled.");
                enabled = true;
                gen2 = GC.CollectionCount(2);
                gen1 = GC.CollectionCount(1);
                new GCBeep();
            }
        }
        private static int  gen1, gen2;
        ~GCBeep()
        {

            if (enabled && !(Environment.HasShutdownStarted || AppDomain.CurrentDomain.IsFinalizingForUnload()))
            {
                if (DateTime.Now.TimeOfDay.TotalSeconds - lastCollection.TimeOfDay.TotalSeconds < 1)
                {                    
                    System.Console.Beep(100, 150);
                    System.Console.Beep(37, 100);
                    System.Console.Beep(100, 250);
                }
                else
                {                    
                    System.Console.Beep(1000, 200);
                }
                

                if (gen2 < GC.CollectionCount(2))
                {
                    gen2 = GC.CollectionCount(2);
                    gen1= GC.CollectionCount(1);
                    
                    Trace.WriteLine("Generation 2 garbage collection at " + DateTime.Now.ToString());
                }
                else if (gen1 < GC.CollectionCount(1))
                {
                    gen1 = GC.CollectionCount(1);
                    Trace.WriteLine("Generation 1 garbage collection at " + DateTime.Now.ToString());
                }
                else
                {
                    Trace.WriteLine("Generation 0 garbage collection at " + DateTime.Now.ToString());
                }
                lastCollection = DateTime.Now;
                
                new GCBeep();
            }
        }
    }
}
